﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DawnOfConquest
{
    class Attack : DawnOfConquest.Ability
    {
        
       public Attack(Piece user)
        {
            range = user.getRange() ;
            targetType = "Enemy";
            name = "Melee Attack";
        }

        public override String execute(Piece user, Piece target)
        {
            String result;
            Random r = new Random();
            int chanceToHit;

            if (user.getName().Equals("Nomad") || user.getName().Equals("Archer"))
            {
                result = user.getFaction() + " " + user.getName();
                if (user.getName().Equals("Archer"))
                {
                    chanceToHit = 99 - 5 * (target.getDefense()/2 - user.getAttack());
                }
                else
                {
                    chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());
                }

                if (r.Next(100) < chanceToHit)
                {
                    result += " hits for " + user.getDamage();
                    target.applyDamage(user.getDamage());
                }
                else
                    result += " misses";
                if (target.getHealth() < 1)
                {
                    result += ", " + target.getFaction() + " " + target.getName();
                    result += " is killed";
                }
            }
            else
            {
                if (target.getName().Equals("Pikeman") && !user.getName().Equals("Pikeman"))
                {
                    result = target.getFaction() + " " + target.getName();
                    chanceToHit = 99 - 5 * (user.getDefense() - target.getAttack());

                    if (r.Next(100) < chanceToHit)
                    {
                        if (user.hasPieceMoved())
                        {
                            result += " hits for " + target.getDamage() * 2;
                            user.applyDamage(target.getDamage() * 2);
                        }
                        else
                        {
                            result += " hits for " + target.getDamage();
                            user.applyDamage(target.getDamage());
                        }
                    }
                    else
                        result += " misses";
                    result += ", " + user.getFaction() + " " + user.getName();
                    if (user.getHealth() > 0)
                    {
                        chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());
                        result += "'s retailiation";
                        if (r.Next(100) < chanceToHit)
                        {
                            result += " hits for " + user.getDamage() / 2;
                            target.applyDamage(user.getDamage() / 2);
                        }
                        else
                            result += " misses";
                    }
                    else
                        result += " is killed";
                }
                else if (user.getName().Equals("Swordsman"))
                {
                    result = user.getFaction() + " " + user.getName();
                    chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());

                    if (r.Next(100) < chanceToHit)
                    {
                        result += " hits for " + user.getDamage();
                        target.applyDamage(user.getDamage());
                    }
                    else
                        result += " misses";
                    result += ", " + target.getFaction() + " " + target.getName();
                    if (target.getHealth() > 0)
                    {
                        chanceToHit = 99 - 5 * (user.getDefense() - target.getAttack());
                        result += "'s retailiation";
                        if (r.Next(100) < chanceToHit)
                        {
                            result += " hits for " + target.getDamage() / 2;
                            user.applyDamage(target.getDamage() / 2);
                        }
                        else
                            result += " misses";
                        if (user.getHealth() < 1)
                        {
                            result += ", " + user.getFaction() + " " + user.getName();
                            result += " is killed";
                        }
                    }
                    else
                        result += " is killed";

                    result += user.getFaction() + " " + user.getName();
                    chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());

                    if (r.Next(100) < chanceToHit)
                    {
                        result += " hits for " + user.getDamage();
                        target.applyDamage(user.getDamage());
                    }
                    else
                        result += " misses";
                    if (target.getHealth() < 1)
                    {
                        result += ", " + target.getFaction() + " " + target.getName();
                        result += " is killed";
                    }
                }
                else if (user.getName().Equals("Knight"))
                {
                    result = user.getFaction() + " " + user.getName();
                    chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());

                    if (r.Next(100) < chanceToHit)
                    {
                        result += " hits for " + user.getDamage();
                        target.applyDamage(user.getDamage());
                    }
                    else
                        result += " misses";
                    result += ", " + target.getFaction() + " " + target.getName();
                    if (target.getHealth() > 0)
                    {
                        if (!(user.hasPieceMoved() && r.Next(2) == 1))
                        {
                            chanceToHit = 99 - 5 * (user.getDefense() - target.getAttack());
                            result += "'s retailiation";
                            if (r.Next(100) < chanceToHit)
                            {
                                result += " hits for " + target.getDamage() / 2;
                                user.applyDamage(target.getDamage() / 2);
                            }
                            else
                                result += " misses";
                            if (user.getHealth() < 1)
                            {
                                result += ", " + user.getFaction() + " " + user.getName();
                                result += " is killed";
                            }
                        }
                        else
                            result += "loses retailiation to charge";
                    }
                    else
                        result += " is killed";
                }
                else
                {
                    result = user.getFaction() + " " + user.getName();
                    chanceToHit = 99 - 5 * (target.getDefense() - user.getAttack());

                    if (r.Next(100) < chanceToHit)
                    {
                        result += " hits for " + user.getDamage();
                        target.applyDamage(user.getDamage());
                    }
                    else
                        result += " misses";
                    result += ", " + target.getFaction() + " " + target.getName();
                    if (target.getHealth() > 0)
                    {
                        chanceToHit = 99 - 5 * (user.getDefense() - target.getAttack());
                        result += "'s retailiation";
                        if (r.Next(100) < chanceToHit)
                        {
                            result += " hits for " + target.getDamage() / 2;
                            user.applyDamage(target.getDamage() / 2);
                        }
                        else
                            result += " misses";
                        if (user.getHealth() < 1)
                        {
                            result += ", " + user.getFaction() + " " + user.getName();
                            result += " is killed";
                        }
                    }
                    else
                        result += " is killed";
                }

            }
          
            if (target.getHealth() < 1)         //if target dies give user extra xp, else give the target xp. Prevents reviving dead units with lvling up
                user.addXP(80 * target.getLevel());         //amount of xp is determined by level
            else
                target.addXP(10 * user.getLevel());

            if (user.getHealth() < 1)
                target.addXP(55 * user.getLevel());
            else
                user.addXP(40 * target.getLevel());

            return result;
        }
    }
}
